﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;

namespace Smart.Editor
{
	public class ExcelConvertEditorWindow : EditorWindow 
	{
		[MenuItem ("Tools/表格/Excel转表")]
		public static void AddWindow ()
		{       
			//创建窗口
			Rect  wr = new Rect (0,0,316,420);
			ExcelConvertEditorWindow window = (ExcelConvertEditorWindow)EditorWindow.GetWindowWithRect (typeof (ExcelConvertEditorWindow),wr,true,"客户端-转表");	
			window.Show();
		}

        [Serializable]
		public class ConvertInfo
		{
			public bool bSelected = false;
			public string name = string.Empty;
			public string lowerName = string.Empty;
			public string path = string.Empty;
            public string subdirName = string.Empty;
            public int mode = 0;//0 editor 1 model 2 hotfix
		}

		protected string filter = string.Empty;
		protected List<ConvertInfo> fileNames = new List<ConvertInfo>();

		Vector2 _scrollPos = Vector2.zero;

		HashSet<string> mIgnoreList = new HashSet<string>();
        protected void LoadIgnoreList()
        {
            mIgnoreList.Clear();
            var path = System.IO.Path.GetFullPath($"{Application.dataPath}/../../Table/ignore_table.txt");
            var lines = System.IO.File.ReadAllLines(path);
            foreach (var name in lines)
            {
                if (!string.IsNullOrEmpty(name))
                    mIgnoreList.Add(name);
            }
        }

        protected void OnEnable()
		{
            LoadIgnoreList();
            _ListAllExcelFiles();
		}

		void _ListAllExcelFiles()
		{
			fileNames.Clear ();
            var dir = System.IO.Path.GetFullPath($"{Application.dataPath}/../../Table/");
            var subdirs = System.IO.Directory.GetDirectories(dir);
            foreach(var curDir in subdirs)
            {
                string[] strList = Directory.GetFiles(curDir, "*.xls", SearchOption.TopDirectoryOnly);
                var subDirName = Path.GetFileNameWithoutExtension(curDir);
                int mode = 0;
                if (subDirName.Equals("Model"))
                    mode = 1;
                else if (subDirName.Equals("Hotfix"))
                    mode = 2;

                for (int i = 0; i < strList.Length; ++i)
                {
                    var filePath = strList[i];
                    var name = Path.GetFileNameWithoutExtension(filePath);
                    if (mIgnoreList.Contains(name))
                    {
                        continue;
                    }
                    fileNames.Add(new ConvertInfo
                    {
                        bSelected = false,
                        name = name,
                        lowerName = name.ToLower(),
                        path = filePath,
                        subdirName = subDirName,
                        mode = mode,
                    });
                }
            }
		}

        protected void OnGUI()
		{
			EditorGUILayout.BeginHorizontal ();
			EditorGUILayout.LabelField ("过滤器",GUILayout.Width(60));
			filter = EditorGUILayout.TextField(filter, GUILayout.Width(100)).ToLower();
			EditorGUILayout.EndHorizontal ();

			EditorGUILayout.BeginHorizontal ();
			if(GUILayout.Button("刷新",GUILayout.Width(100)))
			{
				_ListAllExcelFiles ();
			}
			if (GUILayout.Button ("全选", GUILayout.Width (100))) 
			{
				for (int i = 0; i < fileNames.Count; ++i) 
				{
					fileNames [i].bSelected = true;
				}
			}
			if (GUILayout.Button ("反选", GUILayout.Width (100))) 
			{
				for (int i = 0; i < fileNames.Count; ++i) 
				{
					fileNames [i].bSelected = !fileNames[i].bSelected;
				}
			}
			EditorGUILayout.EndHorizontal ();

			EditorGUILayout.BeginHorizontal();
			if (GUILayout.Button("转表", GUILayout.Width(100)))
			{
				bool choiced = false;
                for (int i = 0; i < fileNames.Count; ++i)
                {
					var fileInfo = fileNames[i];
					if (!fileInfo.bSelected)
						continue;

					choiced = true;
					var excelConverter = new ExcelConvert();
					excelConverter.LoadFile(fileInfo.path,fileInfo.mode);
				}
				if (choiced)
				{
					AssetDatabase.Refresh();
				}
			}
            EditorGUILayout.EndHorizontal();

			//excel-table-name
			_scrollPos = EditorGUILayout.BeginScrollView(_scrollPos);
			EditorGUILayout.BeginVertical ();
			var defColor = GUI.color;
			for (int i = 0; i < fileNames.Count; ++i) 
			{
				if (!fileNames [i].lowerName.Contains(filter)) 
				{
					continue;
				}

                EditorGUILayout.BeginHorizontal();
                if (i % 2 == 0)
                {
                    GUI.color = Color.yellow;
                }
                else
                {
                    GUI.color = Color.cyan;
                }
                EditorGUILayout.LabelField(fileNames[i].name);
                fileNames[i].bSelected = EditorGUILayout.Toggle(fileNames[i].bSelected);

				if(GUILayout.Button("前往>>"))
				{
					System.Diagnostics.Process.Start("Explorer", "/select,"+ fileNames[i].path);
				}

                EditorGUILayout.EndHorizontal();
            }
			GUI.color = defColor;
			EditorGUILayout.EndVertical ();
			EditorGUILayout.EndScrollView ();
		}
	}
}